package com.example.xingzai.myapplication;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class SecondActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText editText02;
    private TextView textView02;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);


        editText02 = (EditText) findViewById(R.id.edit02);
        textView02 = (TextView) findViewById(R.id.text02);

        findViewById(R.id.but01).setOnClickListener(this);
        findViewById(R.id.but02).setOnClickListener(this);
        sharedPreferences = getSharedPreferences("sss", Context.MODE_PRIVATE);
        editor=sharedPreferences.edit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);
        return true;
    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.but01:
                String content01 = editText02.getText().toString().trim();
                editText02.setText("");
                editor.putString("key", content01);
                editor.commit();
                break;
            case R.id.but02:
                String content02 = sharedPreferences.getString("key", "default");
                textView02.setText(content02);
                break;
            default:
                break;
        }
    }
}
